package com.sunjob.mroassets.mapper;

import com.sunjob.common.domain.assets.Accessory;
import com.sunjob.common.domain.assets.Instance;
import com.sunjob.common.domain.assets.Part;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface InstanceMapper {

    //向数据库的实例表中存入实例信息
    @Insert("insert into t_instance(instance_id,instance_name,shape_id,shape_name,instance_vender,instance_brand,instance_model," +
            "instance_mileage,time,instance_create_date,create_date,modify_date,creator,modifier,status) " +
            "values(#{instanceId},#{instanceName},#{shapeId},#{shapeName},#{instanceVender},#{instanceBrand},#{instanceModel},"+
            "#{instanceMileage},#{time},#{instanceCreateDate},#{createDate},#{modifyDate},#{creator},#{modifier},#{status})")
    void insertInstance(Instance instance1);

    //向数据库的部件表中存入部件信息
    @Insert("insert into t_part(part_id,part_name,part_vender,part_brand,part_model,shape2_id,time,last_time,instance_id," +
            "create_date,modify_date,creator,modifier,status) " +
            "values(#{partId},#{partName},#{partVender},#{partBrand},#{partModel},#{shape2Id},#{time},#{lastTime},#{instanceId}," +
            "#{createDate},#{modifyDate},#{creator},#{modifier},#{status})")
    void insertPart(Part part);

//    @Insert("insert into t_accessory " +
//            "values <foreach collection=\"list\" item=\"accessory\" index=\"index\" separator=\",\">" +
//            "</foreach>")
//    void insertAccessorys(List<Accessory> accessorys);

    //向数据库的零件表中存入零件信息
    @Insert("insert into t_accessory(accessory_id,accessory_name,part_id,accessory_model,accessory_specification," +
            "accessory_vender,accessory_area,time,last_time,accessory_create_date,create_date,modify_date,creator,modifier,status) " +
            " values(#{accessoryId},#{accessoryName},#{partId},#{accessoryModel},#{accessorySpecification},#{accessoryVender}," +
            "#{accessoryArea},#{time},#{lastTime},#{accessoryCreateDate},#{createDate},#{modifyDate},#{creator},#{modifier},#{status})")
    void insertAccessory(Accessory accessory);

    //从数据库实例表中查询实例信息
    @Select("select instance_id,instance_name,shape_id,shape_name,instance_vender,instance_brand,instance_model,instance_create_date," +
            "instance_mileage,time,create_date,modify_date,creator,modifier,status from t_instance where instance_id = #{entityId}")
    @Results({
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "instanceName",column = "instance_name"),
            @Result(property = "shapeId",column = "shape_id"),
            @Result(property = "shapeName",column = "shape_name"),
            @Result(property = "instanceVender",column = "instance_vender"),
            @Result(property = "instanceBrand",column = "instance_brand"),
            @Result(property = "instanceModel",column = "instance_model"),
            @Result(property = "instanceMileage",column = "instance_mileage"),
            @Result(property = "time",column = "time"),
            @Result(property = "instanceCreateDate",column = "instance_create_date"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    Instance queryInstanceById(Long entityId);

    //从数据库部件表中查询部件信息
    @Select("select part_id,part_name,part_vender,part_brand,part_model,time,last_time,shape2_id,instance_id," +
            "create_date,modify_date,creator,modifier,status " +
            "from t_part where instance_id = #{instanceId}")
    @Results({
            @Result(property = "partId",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "partVender",column = "part_vender"),
            @Result(property = "partBrand",column = "part_brand"),
            @Result(property = "partModel",column = "part_model"),
            @Result(property = "time",column = "time"),
            @Result(property = "lastTime",column = "last_time"),
            @Result(property = "shape2Id",column = "shape2_id"),
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    List<Part> queryPartsByInstanceId(Long instanceId);

    //从数据库零件表中查询零件信息
    @Select("select accessory_id,accessory_name,part_id,accessory_model,accessory_specification,accessory_create_date," +
            "accessory_vender,accessory_area,time,last_time,create_date,modify_date,creator,modifier,status " +
            "from t_accessory where part_id = #{partId}")
    @Results({
            @Result(property = "accessoryId",column = "accessory_id"),
            @Result(property = "accessoryName",column = "accessory_name"),
            @Result(property = "partId",column = "part_id"),
            @Result(property = "accessoryModel",column = "accessory_model"),
            @Result(property = "accessorySpecification",column = "accessory_specification"),
            @Result(property = "accessoryVender",column = "accessory_vender"),
            @Result(property = "accessoryArea",column = "accessory_area"),
            @Result(property = "time",column = "time"),
            @Result(property = "lastTime",column = "last_time"),
            @Result(property = "accessoryCreateDate",column = "accessory_create_date"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    List<Accessory> queryAccessoryByPartId(Long partId);

    //通过部件id从数据库实例表中查询实例信息
    @Select("select instance_id,instance_name,shape_id,shape_name,instance_vender,instance_brand,instance_model,instance_create_date," +
            "instance_mileage,time,create_date,modify_date,creator,modifier,status from t_instance where instance_id = " +
            "(select instance_id from t_part where part_id = #{entityId})")
    @Results({
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "instanceName",column = "instance_name"),
            @Result(property = "shapeId",column = "shape_id"),
            @Result(property = "shapeName",column = "shape_name"),
            @Result(property = "instanceVender",column = "instance_vender"),
            @Result(property = "instanceBrand",column = "instance_brand"),
            @Result(property = "instanceModel",column = "instance_model"),
            @Result(property = "instanceMileage",column = "instance_mileage"),
            @Result(property = "time",column = "time"),
            @Result(property = "instanceCreateDate",column = "instance_create_date"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    Instance queryInstanceByPartId(Long entityId);

    @Select("select instance_id,instance_name,shape_id,shape_name,instance_vender,instance_brand,instance_model,instance_create_date," +
            "instance_mileage,time,create_date,modify_date,creator,modifier,status from t_instance")
    @Results({
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "instanceName",column = "instance_name"),
            @Result(property = "shapeId",column = "shape_id"),
            @Result(property = "shapeName",column = "shape_name"),
            @Result(property = "instanceVender",column = "instance_vender"),
            @Result(property = "instanceBrand",column = "instance_brand"),
            @Result(property = "instanceModel",column = "instance_model"),
            @Result(property = "instanceMileage",column = "instance_mileage"),
            @Result(property = "time",column = "time"),
            @Result(property = "instanceCreateDate",column = "instance_create_date"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    List<Instance> getAllInstance();
    @Select("select part_id,part_name,part_vender,part_brand,part_model,time,last_time,shape2_id,instance_id," +
            "create_date,modify_date,creator,modifier,status from t_part")
    @Results({
            @Result(property = "partId",column = "part_id"),
            @Result(property = "partName",column = "part_name"),
            @Result(property = "partVender",column = "part_vender"),
            @Result(property = "partBrand",column = "part_brand"),
            @Result(property = "partModel",column = "part_model"),
            @Result(property = "time",column = "time"),
            @Result(property = "lastTime",column = "last_time"),
            @Result(property = "shape2Id",column = "shape2_id"),
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    List<Part> getAllPart();

    @Select("select instance_id,instance_name,shape_id,shape_name,instance_vender,instance_brand,instance_model,instance_create_date," +
            "instance_mileage,time,create_date,modify_date,creator,modifier,status from t_instance where shape_id = #{shapeId}")
    @Results({
            @Result(property = "instanceId",column = "instance_id"),
            @Result(property = "instanceName",column = "instance_name"),
            @Result(property = "shapeId",column = "shape_id"),
            @Result(property = "shapeName",column = "shape_name"),
            @Result(property = "instanceVender",column = "instance_vender"),
            @Result(property = "instanceBrand",column = "instance_brand"),
            @Result(property = "instanceModel",column = "instance_model"),
            @Result(property = "instanceMileage",column = "instance_mileage"),
            @Result(property = "time",column = "time"),
            @Result(property = "instanceCreateDate",column = "instance_create_date"),
            @Result(property = "createDate",column = "create_date"),
            @Result(property = "modifyDate",column = "modify_date"),
            @Result(property = "creator",column = "creator"),
            @Result(property = "modifier",column = "modifier"),
            @Result(property = "status",column = "status")
    })
    List<Instance> getInstanceByShapeId(Long shapeId);
}
